﻿//
// Created by Grishul Eugeny
//
// See license in License.txt
//

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Diagnostics;
using NCommander.Core;
using NCommander.NodeProviders;

namespace NCommander
{
	public partial class MainForm : Form
	{
		private PanelView _activePanel, _passivePanel;

		public MainForm()
		{
			InitializeComponent();

			_leftPanel.CreateTab( new ServicesView() );
			_rightPanel.CreateTab( new RegistryView() );
		}

		private void editToolStripMenuItem_Click( object sender, EventArgs e )
		{
			_activePanel.CurrentProvider.OnEdit( _activePanel.SelectedNodes );
		}

		private void diffToolStripMenuItem_Click( object sender, EventArgs e )
		{
			var winMerge = string.Join( Path.DirectorySeparatorChar.ToString(), new[] { Environment.GetFolderPath( Environment.SpecialFolder.ProgramFiles ), "winmerge", "winmergeu.exe" } );

			if( File.Exists( winMerge ) )
			{
				string leftFile, rightFile;

				if( _leftPanel.CurrentProvider is FileSystemProvider )
					leftFile = _leftPanel.CurrentProvider.CurrentNode.Path;
				else
				{
					leftFile = Path.GetTempFileName();
					using( var writer = new StreamWriter( leftFile ) )
						_leftPanel.CurrentProvider.CreateTextRepresentation( writer );
				}

				if( _rightPanel.CurrentProvider is FileSystemProvider )
					rightFile = _rightPanel.CurrentProvider.CurrentNode.Path;
				else
				{
					rightFile = Path.GetTempFileName();
					using( var writer = new StreamWriter( rightFile ) )
						_rightPanel.CurrentProvider.CreateTextRepresentation( writer );
				}

				Process.Start( winMerge, string.Format( "/s /r /e /maximize {0} {1}", leftFile, rightFile ) );
			}
		}

		private void selectAllToolStripMenuItem_Click( object sender, EventArgs e )
		{
			_activePanel.SelectAll();
		}

		private void _leftPanel_Enter( object sender, EventArgs e )
		{
			_activePanel = _leftPanel;
			_passivePanel = _rightPanel;
		}

		private void _rightPanel_Enter( object sender, EventArgs e )
		{
			_passivePanel = _leftPanel;
			_activePanel = _rightPanel;
		}

		private void deleteToolStripMenuItem_Click( object sender, EventArgs e )
		{
			bool confirmed = false;
			var nodes = _activePanel.SelectedNodes;
			var names = string.Join( ", ", nodes.Select( x => x.Path ).ToArray() );

			if( nodes.Length == 1 )
				confirmed = MessageBox.Show( this, string.Format( Resources.Services.Data.DeleteServicePrompt, names ), null, MessageBoxButtons.YesNo ) == DialogResult.Yes;
			else
				confirmed = MessageBox.Show( this, string.Format( Resources.Services.Data.DeleteServicesPrompt, names ), null, MessageBoxButtons.YesNo ) == DialogResult.Yes;

			if( confirmed )
				_activePanel.CurrentProvider.OnDelete( nodes );
		}
	}
}
